
LyLock_Summon = { }
LyLock_Summon.Frame = "LyLockSummonFrame"
LyLock_Summon.Button = "LyLockSummonButton"
LyLock_Summon.Events = {
    "COMBAT_LOG_EVENT_UNFILTERED",
    "UNIT_SPELLCAST_SENT",
    "UNIT_SPELLCAST_CHANNEL_STOP",
    "UNIT_SPELLCAST_FAILED",
    "UNIT_SPELLCAST_INTERRUPTED"
}


table.insert(LyLock_Plugins, LyLock_Summon)

------------------------------------------------------
-- Summon Settings -----------------------------------
------------------------------------------------------
LyLock_Defaults.Summon_Message1 = "Summoning %target%, two players need click the portal plz";
LyLock_Defaults.Summon_Failed1 = "Failed to Summon %target%, plz don't move during the ritual";
LyLock_Defaults.Summon_Message2 = "OMG! Where did this horse come from!";


LyLock_Summon.OnSettings = function(action)
    if action=="default" then
            
        LyLock_Settings.Summon_Button = ReturnSetting(LyLock_Settings.Summon_Button, true)
        LyLock_Settings.Summon_Casting = ReturnSetting(LyLock_Settings.Summon_Casting, true)
        LyLock_Settings.Summon_CastFail = ReturnSetting(LyLock_Settings.Summon_CastFail, false)
        LyLock_Settings.Summon_Mounting = ReturnSetting(LyLock_Settings.Summon_Mounting, false)
        LyLock_Settings.Summon_Message1 = ReturnSetting(LyLock_Settings.Summon_Message1, LyLock_Defaults.Summon_Message1)
        LyLock_Settings.Summon_Failed1 = ReturnSetting(LyLock_Settings.Summon_Failed1, LyLock_Defaults.Summon_Failed1)
        LyLock_Settings.Summon_Message2 = ReturnSetting(LyLock_Settings.Summon_Message2, LyLock_Defaults.Summon_Message2)
        
    elseif action=="save" then
        LyLock_Settings.Summon_Button = iif(Summon_Button:GetChecked(), true, false);
        LyLock_Settings.Summon_Casting = iif(Summon_Casting:GetChecked(), true, false);
        LyLock_Settings.Summon_CastFail = iif(Summon_CastFail:GetChecked(), true, false);
        LyLock_Settings.Summon_Mounting = iif(Summon_Mounting:GetChecked(), true, false);
        LyLock_Settings.Summon_Message1 = ReturnSetting(Summon_Message1:GetText(), LyLock_Defaults.Summon_Message1);
        LyLock_Settings.Summon_Failed1 = ReturnSetting(Summon_Failed1:GetText(), LyLock_Defaults.Summon_Failed1);
        LyLock_Settings.Summon_Message2 = ReturnSetting(Summon_Message2:GetText(), LyLock_Defaults.Summon_Message2);
        
    elseif action=="load" then
    
        Summon_Button:SetChecked(LyLock_Settings.Summon_Button);
        Summon_Casting:SetChecked(LyLock_Settings.Summon_Casting);
        Summon_Mounting:SetChecked(LyLock_Settings.Summon_Mounting);
        Summon_Message1:SetText(LyLock_Settings.Summon_Message1);
        Summon_Failed1:SetText(LyLock_Settings.Summon_Failed1);
        Summon_Message2:SetText(LyLock_Settings.Summon_Message2);
        
    end
end


--------------------------------------------------------------------------------------
-- Summon Functions ------------------------------------------------------------------
--------------------------------------------------------------------------------------

LyLock_Summon.Casting = 0
LyLock_Summon.Shards = 0
LyLock_Summon.Player = nil

      
LyLock_Summon.ButtonSetupAction = function()
    local rank = GetSpellRank(WORD_RITUALSUMMON)
    if rank~=nil then
        LyLockSummonButton:SetAttribute("type", "spell");
        LyLockSummonButton:SetAttribute("spell", WORD_RITUALSUMMON.."("..rank..")")
    end
end

LyLock_Summon.ButtonOnMouseDown = function()
    if (UnitIsPlayer("target") and ((UnitInParty("target") or UnitInRaid("target")) and (UnitName("target") ~= UnitName("player")))) then
        if CheckInteractDistance("target", 4)==nil then
            LyLockSummonButton:SetAttribute("type", "spell")
        else
            LyLockSummonButton:SetAttribute("type", "")
            EchoMessage("LyLock", "The targeted player is already near you", "Canceled")
        end
    else
        LyLockSummonButton:SetAttribute("type", "")
        EchoMessage("LyLock", "Target a player in the raid or party to summon", "Canceled")
    end
end


LyLock_Summon.OnEvent = function()

	if event == "COMBAT_LOG_EVENT_UNFILTERED" and (arg2 == "SPELL_AURA_APPLIED" or arg2 == "SPELL_CREATE") and arg7 == UnitName("player") then -- Check if we gain dreadseed/felsteed
		if (string.find(arg10,WORD_SUMMONDREADSTEED) or string.find(arg10,WORD_SUMMONFELSTEED)) then
            if LyLock_Settings.Summon_Mounting then
                SendChatMessage(LyMsg(LyLock_Settings.Summon_Message2, ""),"SAY")
            end
		-- or are we creating a summoning portal?
        elseif (string.find(arg10, WORD_RITUALSUMMON)) then
            LyLock_Summon.Casting = 1
            LyLock_Summon.Shards = SoulShardCount()
            if LyLock_Settings.Summon_Casting then
                Speak(LyMsg(LyLock_Settings.Summon_Message1, LyLock_Summon.Player))
            end
	    end
    else
        if arg1=="player" then
        	-- when Ritual of Summoning is started get the target (user might click away later)
        	if (event == "UNIT_SPELLCAST_SENT") and (string.find(arg2, WORD_RITUALSUMMON)) then
        		LyLock_Summon.Player = UnitName("target")
        	
        	-- check when we end casting if the summon was successful
            elseif ((event == "UNIT_SPELLCAST_CHANNEL_STOP") and (LyLock_Summon.Casting == 1)) then
                LyLock_Summon.Casting = 0
                if (LyLock_Summon.Shards==SoulShardCount()) then
                    if LyLock_Settings.Summon_CastFail then
                        Speak(LyMsg(LyLock_Settings.Summon_Failed1, LyLock_Summon.Player))
                    else
                        EchoMessage("LyLock", "Failed to Summon "..LyLock_Summon.Player, "Canceled")
                    end
                else
                    EchoMessage("LyLock", "Summon for "..LyLock_Summon.Player.." completed", "Success")
                end
                LyLock_Summon.Player = nil
            elseif (((event == "UNIT_SPELLCAST_FAILED") or (event == "UNIT_SPELLCAST_INTERRUPTED")) and (LyLock_Summon.Casting~=0)) then
                LyLock_Summon.Casting= 0
                LyLock_Summon.Player = nil
            end
        end
    end
end